package com.py.nginxanalysis.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class MessageUtil {
    public static String messageService(){
        CloseableHttpResponse response = null;
        String str = "";
        try{
            CloseableHttpClient httpClient = HttpClients.createDefault();

            //appkey和appid需等待申请完毕
            //sit环境
            String accessTokenUrl = "http://172.16.243.7:8080/bap/gw/aopoauth/oauth/token?app_key=76e81081207ec6c729ff087777c6e7e4_type=client_credentials&app_id=10344";
            //uat环境
            //String accessTokenUtl = "http://172.16.243.8:8080/bap/gw/aopoauth/oauth/token?app_key=76e81081207ec6c729ff087777c6e7e4&grant_type=client_credentials&app_id=10344";
            //prd环境
            //String accessTokenUtl = "http://172.16.243.9:8080/bap/gw/aopoauth/oauth/token?app_key=76e81081207ec6c729ff087777c6e7e4&grant_type=client_credentials&app_id=10344";
            HttpGet httpGet = new HttpGet(accessTokenUrl);
            String accessTokenReturnResult = EntityUtils.toString(httpClient.execute(httpGet).getEntity(), "utf-8");
            System.out.println("获取访问M_ABILITY_UNIDOC_105000003接口的accessToken返回结果：" + accessTokenReturnResult);
            JSONObject obj = JSON.parseObject(accessTokenReturnResult);

            //accessToken
            String stoken = obj.get("access_token").toString();

            /*
                系统参数
             */
            Map<String,String> sysParams = new HashMap<>();
            // 接口名称需申请
            sysParams.put("method","");
            sysParams.put("format","json");
            //appId需申请
            sysParams.put("appId","");
            sysParams.put("version","V1.0");
            sysParams.put("accessToken",stoken);
            sysParams.put("sign","SHA");
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
            sysParams.put("timestamp", format.format(new Date()));

            /*
                业务参数
             */
            Map<String,String> busiParams = new HashMap<>();
            busiParams.put("ynTimeSend","N");
            // 实际部署时，找书朋要手机号
            busiParams.put("receiveNums","17601013570");
            //默认值=Y，短信发送只能使用模板！
            busiParams.put("ynTemplate","Y");
            //模板编号：短信模板需在统一通知进行配置，并在平台获取模板ID
            busiParams.put("templateNum","123");
            busiParams.put("ynReply","N");
            //业务系统编码=appid
            busiParams.put("appCode","10344");
            //省公司编码
            busiParams.put("provinceCode","");


            /*
                短信服务接口地址需申请，后面需要该下面的url
             */
            //sit环境
              String url = "http://172.16.243.7:8080/bap/gw/updown/iuMZZr/";
            //uat环境
            //String url = "http://172.16.243.8:8080/bap/gw/updown/iuMZZr/";
            //prd环境
            //String url = "http://172.16.243.9:8080/bap/gw/updown/iuMZZr/";

            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader("Content-Type","application/json");
            //处理系统参数
            if (sysParams != null && !sysParams.isEmpty()) {
                Iterator i$ = sysParams.entrySet().iterator();
                while (i$.hasNext()) {
                    Map.Entry<String, String> e = (Map.Entry) i$.next();
                    httpPost.setHeader(e.getKey(), e.getValue());
                }

            }
            //发送Json格式的数据请求
            StringEntity entity = new StringEntity(JSONObject.toJSONString(busiParams), ContentType.parse("utf-8"));
            entity.setContentType("application/json");
            entity.setContentEncoding("utf-8");
            httpPost.setEntity(entity);
            // 执行http请求
            response = httpClient.execute(httpPost);
            if (200 == response.getStatusLine().getStatusCode()) {
                HttpEntity resEntity = response.getEntity();
                str = EntityUtils.toString(resEntity);
            }

        } catch (ParseException | IOException e) {
            e.printStackTrace();
        }
        return str;
    }
}
